﻿#include "ControlDB.h"

#include <QDebug>
#include <QString>

ControlDB::ControlDB(QObject *parent)
	: QObject(parent), resDb_(nullptr)
{}

ControlDB::~ControlDB()
{}

bool ControlDB::ConnectNewDatabase(const QString& _dbFilePath)
{
	if (resDb_ != nullptr)
	{
		resDb_->close();
		delete resDb_;
		resDb_ = nullptr;
	}
	// 其实账号密码没用
	resDb_ = new QSqlDatabase();
	*resDb_ = QSqlDatabase::addDatabase("QSQLITE");
	resDb_->setDatabaseName(_dbFilePath);
	//resDb_->setUserName("Luster");
	//resDb_->setPassword("123456");

	return resDb_->open();
}

ResourceDataList ControlDB::QueryPictureDataList()
{
	ResourceDataList resDataList;
	QSqlQuery queryPic(*resDb_);
	queryPic.exec("SELECT * FROM PngData");
	while (queryPic.next())
	{
		ResourceDataSPtr curData = std::make_shared<ResourceData>();
		curData->id = queryPic.value("id").toInt();
		curData->name = queryPic.value("name").toString();
		curData->picType = (PictureType)queryPic.value("type").toInt();
		curData->pbgBuffer = QByteArray::fromBase64(queryPic.value("data").toByteArray());
		resDataList.push_back(curData);
	}
	return resDataList;
}

ResourceDataList ControlDB::QueryPictureByName(const QString & _name)
{
	ResourceDataList resDataList;
	QSqlQuery queryPic(*resDb_);
	QString execSqlStr = QString("SELECT * FROM PngData WHERE name LIKE '%%1%'").arg(_name);
	queryPic.exec(execSqlStr);
	while (queryPic.next())
	{
		ResourceDataSPtr curData = std::make_shared<ResourceData>();
		curData->id = queryPic.value("id").toInt();
		curData->name = queryPic.value("name").toString();
		curData->picType = (PictureType)queryPic.value("type").toInt();
		curData->pbgBuffer = QByteArray::fromBase64(queryPic.value("data").toByteArray());
		resDataList.push_back(curData);
	}
	return resDataList;
}

ResourceDataSPtr ControlDB::QueryPicById(const int& _id)
{
	ResourceDataSPtr resData = std::make_shared<ResourceData>();
	QSqlQuery queryPic(*resDb_);
	QString execSqlStr = QString("SELECT * FROM PngData WHERE id='%1'").arg(_id);
	queryPic.exec(execSqlStr);
	if (queryPic.next())
	{
		resData->id = queryPic.value("id").toInt();
		resData->name = queryPic.value("name").toString();
		resData->picType = (PictureType)queryPic.value("type").toInt();
		resData->pbgBuffer = queryPic.value("data").toByteArray();
	}
	return resData;
}

void ControlDB::ImportPicture(const QString& _resName, const QByteArray& _dataBuf, const PictureType& _type)
{
	QSqlQuery queryPic(*resDb_);
	QByteArray base64Pic = _dataBuf.toBase64();
	QString insertSqlStr = QString("INSERT INTO PngData(name,type,data) values ('%1', '%2', '%3');")
		.arg(_resName).arg(_type).arg(base64Pic.data());
	if (!queryPic.exec(insertSqlStr))
	{
		qDebug() << queryPic.lastError();
	}
}

void ControlDB::UpdatePicData(const int& _id, const QString& _newName, const QByteArray& _newData /*= ""*/)
{
	QSqlQuery queryPic(*resDb_);
	QString UpdateSqlStr = "";
	if (_newData.isEmpty())
		UpdateSqlStr = QString("UPDATE PngData SET name='%1' WHERE id='%2'").arg(_newName).arg(_id);
	else
		UpdateSqlStr = QString("UPDATE PngData SET name='%1',data='%2' WHERE id='%3'")
		.arg(_newName).arg(_newData.toBase64().data()).arg(_id);
	if (!queryPic.exec(UpdateSqlStr))
	{
		qDebug() << queryPic.lastError();
	}
}

void ControlDB::DeletePicById(const int& _id)
{
	QSqlQuery queryPic(*resDb_);
	QString insertSqlStr = QString("DELETE FROM PngData WHERE id='%1'").arg(_id);
	if (!queryPic.exec(insertSqlStr))
		qDebug() << queryPic.lastError();
}

ResourceDataList ControlDB::QuerySvgDataList()
{
	ResourceDataList resDataList;
	QSqlQuery querySvg(*resDb_);
	querySvg.exec("SELECT * FROM SvgData");
	while (querySvg.next())
	{
		ResourceDataSPtr curData = std::make_shared<ResourceData>();
		curData->id = querySvg.value("id").toInt();
		curData->name = querySvg.value("name").toString();
		curData->svgContent = querySvg.value("data").toString();
		resDataList.push_back(curData);
	}
	return resDataList;
}

ResourceDataList ControlDB::QuerySvgByName(const QString& _name)
{
	ResourceDataList resDataList;
	QSqlQuery querySvg(*resDb_);
	QString execSqlStr = QString("SELECT * FROM SvgData WHERE name LIKE '%%1%'").arg(_name);
	querySvg.exec(execSqlStr);
	while (querySvg.next()) 
	{
		ResourceDataSPtr curData = std::make_shared<ResourceData>();
		curData->id = querySvg.value("id").toInt();
		curData->name = querySvg.value("name").toString();
		curData->svgContent = querySvg.value("data").toString();
		resDataList.push_back(curData);
	}
	return resDataList;
}

ResourceDataSPtr ControlDB::QuerySvgById(const int& _id)
{
	ResourceDataSPtr resData = std::make_shared<ResourceData>();
	QSqlQuery querySvg(*resDb_);
	QString execSqlStr = QString("SELECT * FROM SvgData WHERE id='%1'").arg(_id);
	querySvg.exec(execSqlStr);
	if (querySvg.next())
	{
		resData->id = querySvg.value("id").toInt();
		resData->name = querySvg.value("name").toString();
		resData->svgContent = querySvg.value("data").toString();
	}
	return resData;
}

void ControlDB::ImportSvg(const QString& _resName, const QString& _fileContent)
{
	QSqlQuery querySvg(*resDb_);
	QString insertSqlStr = QString("INSERT INTO SvgData (name,data) values ('%1','%2')")
		.arg(_resName).arg(_fileContent);
	if (!querySvg.exec(insertSqlStr))
		qDebug() << querySvg.lastError();
}

void ControlDB::UpdateSvgData(const int& _id, const QString& _newName, const QString& _fileContent /*= ""*/)
{
	QSqlQuery querySvg(*resDb_);
	QString insertSqlStr = "";
	if (_fileContent.isEmpty())
		insertSqlStr = QString("UPDATE SvgData SET name='%1' WHERE id='%2'").arg(_newName).arg(_id);
	else
		insertSqlStr = QString("UPDATE SvgData SET name='%1',data='%2' WHERE id='%3'")
		.arg(_newName).arg(_fileContent).arg(_id);
	if (!querySvg.exec(insertSqlStr))
		qDebug() << querySvg.lastError();
}

void ControlDB::DeleteSvgById(const int& _id)
{
	QSqlQuery querySvg(*resDb_);
	QString insertSqlStr = QString("DELETE FROM SvgData WHERE id='%1'").arg(_id);
	if (!querySvg.exec(insertSqlStr))
		qDebug() << querySvg.lastError();
}
